import { requestGet, requestPost } from '@/api/system/common'

/**
 * 消息通道使用
 * @type {{data(): {set: {}, form: {id: null, setting: string}, show: boolean, saveObj: boolean, loading: boolean}, methods: {submitCU(): void, openDialog(*): void, close(): void}}}
 */
export const edit = {
  data() {
    return {
      show: false,
      form: {
        id: null,
        setting: ''
      },
      loading: false,
      saveObj: false,
      set: {}
    }
  },
  methods: {
    // 打开弹窗
    openDialog(channel) {
      this.show = true
      this.loading = true
      // 初始化设置数据
      requestGet('rest/api/sys/notice/v1/setting/one', { id: channel })
        .then(res => {
          this.form.id = channel
          const { data } = res
          if (undefined !== data && undefined !== data.setting) {
            this.form.setting = data.setting
            this.set = JSON.parse(data.setting)
          }
        }).catch(() => {

        }).finally(() => {
          this.loading = false
        })
    },
    submitCU() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.setting = JSON.stringify(this.set)
          requestPost('rest/api/sys/notice/v1/setting/save/entity', this.form)
            .then(res => {
              this.$message.success('保存成功！')
              this.close()
            }).catch(() => {
              this.$message.error('保存失败！')
            })
        }
      })
    },
    close() {
      this.show = false
    }
  }
}

/**
 * 消息模版
 * @type {{data(): {set: {params: [], content: string}, form: {}, show: boolean, channel: {}, saveObj: boolean, loading: boolean, dbColumn}, methods: {toDelete(*): void, doAdd(): void, submitCU(): void, openDialog(*): void, close(): void}}}
 */
export const template = {
  data() {
    return {
      loading: false,
      show: false,
      set: {
        params: [],
        content: ''
      },
      channel: {}, // 通道
      saveObj: false,
      form: {},
      column: [],
      // 标题，地址，所属系统，发起人，发起人工号，当前步骤，申请时间，当前步骤时间
      dbColumn: ['title', 'url', 'ssxt', 'fqr', 'fqrgh', 'bz', 'sqsj', 'dqbzsj'],
      // 标题，是否打开地址，地址，发布人姓名，工号，消息类型，消息来源，发布时间
      inform: ['title', 'isOpenUrl', 'openUrl', 'username', 'userid', 'type', 'ly', 'time'],
      // 标题，打开地址，所属系统，发起人，发起人工号，申请人，申请人工号，申请时间，当前状态
      ybColumn: ['title', 'url', 'ssxt', 'fqr', 'fqrgh', 'shr', 'shrgh', 'sqsj', 'state'],
      // 标题, 打开地址，来源，发布时间，类型
      linkNotice: ['title', 'url', 'ly', 'time', 'type']
    }
  },
  methods: {
    // 打开弹窗
    async openDialog(data) {
      this.set.params = []
      this.form = data.form
      this.channel = data.channel // 通道
      this.set.content = data.form.content === undefined ? '' : data.form.content
      this.show = true
      if (undefined !== this.form.params && this.form.params !== '' && this.form.params !== null) {
        this.set.params = JSON.parse(this.form.params)
      }
      // 站内信
      if (this.channel.type === 'notice' && this.set.params.length === 0) {
        this.set.params = [
          {
            key: 'title',
            label: '标题',
            type: 1
          },
          {
            key: 'leftLabel',
            label: '左下标题名称',
            type: 2
          },
          {
            key: 'leftKey',
            label: '左下绑定值',
            type: 1
          },
          {
            key: 'rightLabel',
            label: '右下标题名称',
            type: 2
          },
          {
            key: 'rightKey',
            label: '右下绑定值',
            type: 1
          }
        ]
      }
      // 如果是微信消息，直接解析
      if (this.channel.type === 'WCat' || this.channel.type === 'mobile') {
        await requestGet('rest/api/sys/notice/v1/setting/one', { id: this.channel.id })
          .then(res => {
            const { data } = res
            const params = JSON.parse(data.setting)
            let cat = ''
            if (this.channel.type === 'WCat') {
              cat = params.params
            }
            if (this.channel.type === 'mobile') {
              cat = params.param
            }
            if (undefined !== cat && cat !== null && cat !== '') {
              JSON.parse(cat).forEach(item => {
                if (this.set.params.length > 0) {
                  let info = false
                  this.set.params.forEach(obj => {
                    if (obj.key === item.key) {
                      info = true
                    }
                  })
                  if (info === false) {
                    const obj = { key: item.key, column: '' }
                    this.set.params.push(obj)
                  }
                } else {
                  const obj = { key: item.key, column: '' }
                  this.set.params.push(obj)
                }
              })
            }
          })
      }
      // 待办消息
      if (this.form.mblx === 'db') {
        this.column = this.dbColumn
      }
      // 站内信
      if (this.form.mblx === 'inform') {
        this.column = this.inform
      }
      // 已办消息
      if (this.form.mblx === 'yb') {
        this.column = this.ybColumn
      }
      // 链接消息
      if (this.form.mblx === 'linkNotice') {
        this.column = this.linkNotice
      }
    },
    // 提交更新
    submitCU() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.content = this.set.content
          this.form.params = JSON.stringify(this.set.params)
          requestPost('rest/api/sys/notice/v1/temp/update/entity', this.form)
            .then(res => {
              this.$message.success('保存成功！')
              this.close()
            }).catch(() => {
              this.$message.error('保存失败！')
            })
        }
      })
    },
    // 关闭服务
    close() {
      this.show = false
      this.set.content = ''
    },
    //
    doAdd() {
      const item = { key: '', column: '' }
      this.$nextTick(() => {
        this.set.params.push(item)
      })
    },
    toDelete(item) {
      const index = this.set.params.indexOf(item)
      if (index !== -1) {
        this.set.params.splice(index, 1)
      }
    }
  }
}

export const auth = {
  data() {
    return {
      show: false,
      form: {
        id: null,
        setting: ''
      },
      loading: false,
      saveObj: false,
      set: {}
    }
  },
  methods: {
    // 打开弹窗
    openDialog(channel) {
      this.show = true
      this.loading = true
      // 初始化设置数据
      requestGet('rest/api/sys/auth/v1/setting/one', { id: channel })
        .then(res => {
          this.form.id = channel
          const { data } = res
          this.form = data
          if (undefined !== data && undefined !== data.setting && data.setting !== null) {
            this.set = JSON.parse(data.setting)
            console.log(this.set)
          }
        }).catch(() => {

        }).finally(() => {
          this.loading = false
        })
    },
    submitCU() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.setting = JSON.stringify(this.set)
          requestPost('rest/api/sys/auth/v1/setting/update/entity', this.form)
            .then(res => {
              this.$message.success('保存成功！')
              this.close()
            }).catch(() => {
              this.$message.error('保存失败！')
            })
        }
      })
    },
    close() {
      this.show = false
    }
  }
}
